#include "Menu.h"
#include <iostream>
//additional includes
#include <fstream>  //BP A1	//AO A2 //CR A3
#include <algorithm> //CR S1
#include <vector>	 //CR S1

using namespace std;

Menu::Menu()
{
}

void Menu::display_main_menu() const 
{
	cout << "MAIN MENU (Enter a number (1-6) to select an option.)\n" <<"1. Info\t\t" << "2. Read\t\t" << "3. Show\n" << "4. Find\t\t" << "5. Update\t" << "6. Exit\n"; //CR A3
}
void Menu::display_info() const
{
	cout << '\n';  //CR
	cout <<"Project Part I \nTEAM SUPERCOOL \nMembers: Allie Ogden, Brittni Pena, Clifton Rapier\n";	//AO A2
	cout << '\n';  //CR
}

void Menu::read()
{
	cout << "\nReading files: carts.dat, catprd.dat, and cust.dat\n";  //CR
	
	ifstream ist("cust.dat");														//BP A1
	if(!ist.is_open()) throw InvalidFile();											//BP A1
	int n;																			//BP A1
	ist >> n;																		//BP A1
	int Customer_ID;																//BP A1
	string Customer_name;															//BP A1
	int house_number;																//BP A1
	string street_name;																//BP A1
	int zip_code;																	//BP A1
	for (int i=0; i<n; ++i){														//BP A1
		ist>>Customer_ID>>Customer_name>>house_number>>street_name>>zip_code;		//BP A1
		if(ist.fail()) throw InvalidData();											//BP A1
		Address a(house_number, street_name, zip_code);								//BP A1
		Customer c(Customer_ID, Customer_name, a);									//BP A1
		custs.push_back(c);															//BP A1
	}																				//BP A1
	ifstream cart_st ("carts.dat");							//AO A2
	if(!cart_st.is_open()) throw InvalidFile();				//AO A2
	int numRec;												//AO A2
	cart_st >> numRec;										//AO A2
	if(cart_st.fail()) throw InvalidData();					//AO A2
	for(int i = 0; i < numRec; i++){						//AO A2
		int cart_id;										//AO A2
		cart_st >> cart_id;									//AO A2
		if(cart_st.fail()) throw InvalidData();				//AO A2
		int cust_id;										//AO A2
		cart_st >> cust_id;									//AO A2
		if(cart_st.fail()) throw InvalidData();				//AO A2
		Date d;												//AO A2
		cart_st >> d;										//AO A2
		if(cart_st.fail()) throw InvalidData();				//AO A2
		carts.push_back(Cart(cart_id,cust_id,d));			//AO A2
	}														//AO A2
	cart_st >> numRec;										//AO A2
	if(cart_st.fail()) throw InvalidData();					//AO A2
	for(int i = 0; i < numRec; i++){						//AO A2
		int iid;											//AO A2
		cart_st >> iid;										//AO A2
		if(cart_st.fail()) throw InvalidData();				//AO A2
		int cid;											//AO A2
		cart_st >> cid;										//AO A2
		if(cart_st.fail()) throw InvalidData();				//AO A2
		int pid;											//AO A2
		cart_st >> pid;										//AO A2
		if(cart_st.fail()) throw InvalidData();				//AO A2
		int pq;												//AO A2
		cart_st >> pq;										//AO A2
		if(cart_st.fail()) throw InvalidData();				//AO A2
		cart_items.push_back(Cart_item(iid,cid,pid,pq));	//AO A2
	}														//AO A2
	
	ifstream catprd ("catprd.dat");							//CR A3
	if(!catprd.is_open()) throw InvalidFile();				//CR A3
	catprd >> numRec;										//CR A3
	if(catprd.fail()) throw InvalidData();					//CR A3
	int i = 0;												//CR A3
	while(i<numRec){										//CR A3
		int id;												//CR A3
		catprd >> id;										//CR A3
		if(catprd.fail()) throw InvalidData();				//CR A3
		string catname;										//CR A3
		catprd >> catname;									//CR A3
		if(catprd.fail()) throw InvalidData();				//CR A3
		Category temp(id,catname);							//CR A3
		cats.push_back(temp);								//CR A3
		i++;												//CR A3
	}														//CR A3
	catprd >> numRec;										//CR A3
	if(catprd.fail()) throw InvalidData();					//CR A3
	i = 0;													//CR A3
	while(i<numRec){										//CR A3
		int pid;											//CR A3
		catprd >> pid;										//CR A3
		if(catprd.fail()) throw InvalidData();				//CR A3
		int cid;											//CR A3
		catprd >> cid;										//CR A3
		if(catprd.fail()) throw InvalidData();				//CR A3
		string pname;										//CR A3
		catprd >> pname;									//CR A3
		if(catprd.fail()) throw InvalidData();				//CR A3
		double price;										//CR A3
		catprd >> price;									//CR A3
		if(catprd.fail()) throw InvalidData();				//CR A3
		Product temp(pid,cid,pname,price);					//CR A3
		prods.push_back(temp);								//CR A3
		i++;												//CR A3
	}														//CR A3
	
	cout << "Finished reading files successfully.\n\n"; 	//CR
}
	
bool compare_cats (Category a, Category b) {				//CR S1
	return a.get_id() < b.get_id();							//CR S1
}															//CR S1
	
void Menu::show() const 
{
	cout << "\nCategories:\n";  //CR S1
	//sort categories
	vector<Category> temp = cats;							//CR S1
	sort(temp.begin(),temp.end(),compare_cats);				//CR S1
	//print categories
	for(int i = 0; i < temp.size(); i++){					//CR S1
		cout << temp[i].display() << " ";					//CR S1
		int count = 0;										//CR S1
		for(int w = 0; w < prods.size(); w++){				//CR S1
			if(prods[w].get_cat_id() == temp[i].get_id())   //CR S1
				count++;									//CR S1
		}													//CR S1
		cout << count << '\n';								//CR S1
	}														//CR S1
	cout << '\n';  //CR S1
}

void Menu::find() const 
{
	cout << '\n';  //CR S1
	//ask for product
	cout << "Enter product id: ";						//CR S1
	int id;												//CR S1
	cin >> id;											//CR S1
	//find product price in prods
	double price = -1;
	for(int i = 0; i < prods.size(); i++){
		if(prods[i].get_prod_id()==id){
			price = prods[i].get_price();
			break;
		}
	}
	if (price ==-1){
		cout << "No product with matching id found.\n";
		return;
	}
	//look for product in cart_items	
	int count = 0;										//CR S1
	for(int i = 0; i < cart_items.size(); i++){			//CR S1
		if(id == cart_items[i].get_prod_id())     		//CR S1
			count += cart_items[i].get_quantity();		//CR S1
	}													//CR S1
	//print total sales
	cout << "Total sales: $" << count*price << '\n';	//CR S1
	cout << '\n';  //CR S1
}

void Menu::update()  
{
	cout << '\n';  //CR S1
	//prompt
	cout << "Enter a product to add (format: Product_Id Category_Id Product_name Price)\n";	//CR S1
	//read in info for product
	int pid, cid;								//CR S1
	string pname;								//CR S1
	double price;								//CR S1
	cin >> pid >> cid >> pname >> price;		//CR S1
	//make sure category exist
	bool found = false;							//CR S1
	for(int i = 0; i < cats.size(); i++){		//CR S1
		if(cid == cats[i].get_id())				//CR S1
			found = true;						//CR S1
	}											//CR S1
	if(!found)									//CR S1
	{											//CR S1
		cout << "Category does not exist.\n\n";	//CR S1
		return;									//CR S1
	}											//CR S1
	//add a product to prods
	prods.push_back(Product(pid,cid,pname,price));	//CR S1
	cout << "Product added.\n\n";  //CR S1
}
